//
//  ByBaseViewController.m
//  beibei_ios
//
//  Created by hebidu on 16/4/15.
//  Copyright © 2016年 hebidu. All rights reserved.
//

#import "ByBaseViewController.h"

#import "ByTransitionController.h" // 转场动画

#define USE_CUSTOM_TRANSITION 1

@interface ByBaseViewController () 

@property (nonatomic, strong) ByTransitionController *transition;

@end

@implementation ByBaseViewController


/** 视图已经加载 */
- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self registerLJWKeyboardHandler]; // 操作输入框移动
    self.transition = [[ByTransitionController alloc] init]; // 动画控制器
    self.interactiveTransition = [[ByInteractiveController alloc] init]; // 手势互动控制器
    [self.interactiveTransition wireToViewController:self];
    self.view.backgroundColor = [UIColor whiteColor]; // 设置默认颜色
    
}
/** 视图将要出现，在该方法中设置导航栏的变动属性，本基类中定义默认属性，子类中要改变导航栏属性则需要覆写该方法 */
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    // 设置导航栏的默认属性(默认为不透明、白色状态栏)
    [self setNaviAttributeWithTranslucent:NO stateBarLightContent:YES];
    
    // 是首页的三个控制器则显示标签栏
    if ([self.navigationController.viewControllers indexOfObject:self] == 0) {
        // 退出的时候要显示导航栏
        ByTabBarController *tabBarController = (ByTabBarController *)self.navigationController.tabBarController;
        [tabBarController showTabBar];
    }
}
/** 视图已经出现 */
- (void)viewDidAppear:(BOOL)animated {
    
    [super viewDidAppear:animated];
    
    if (USE_CUSTOM_TRANSITION) {
        self.navigationController.delegate = self;
    }
    
}
/** 视图将要消失 */
- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    /** 视图移除后取消请求 */
    [[AFHTTPSessionManager manager].operationQueue cancelAllOperations];
}
/** 视图已经消失 */
- (void)viewDidDisappear:(BOOL)animated {
    
    [super viewDidDisappear:animated];
    
}

#pragma mark - 互动手势
#pragma mark - UINavigationControllerDelegate
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {
    
    BOOL reverse = (operation == UINavigationControllerOperationPop);
    self.transition.reverse = reverse;
    
    return self.transition;

}
// 互动手势
- (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController {
    
    return self.interactiveTransition.interacting ? self.interactiveTransition : nil;
}

#pragma mark - 自定义基类方法
/** 快捷设置导航栏属性
 translucent: YES->半透明 NO->非半透明
 light:       YES->主题色 NO->黑色
 */
- (void)setNaviAttributeWithTranslucent:(BOOL)translucent
                   stateBarLightContent:(BOOL)light {
    
    // 设置状态栏
    [UIApplication sharedApplication].statusBarStyle = light ? UIStatusBarStyleLightContent : UIStatusBarStyleDefault;
    // 设置导航栏透明度
    self.navigationController.navigationBar.translucent = translucent;

    // 去掉导航栏背景色，用背景图代替
    self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
    // 去掉导航栏下部的横线
    self.navigationController.navigationBar.shadowImage = [UIImage imageWithColor:[UIColor clearColor]];
    // 导航栏背景图
    UIImage *bgImage = translucent ? [UIImage imageWithColor:[UIColor clearColor]] : [UIImage imageWithColor:JM_THEME_COLOR];
    [self.navigationController.navigationBar setBackgroundImage:bgImage forBarMetrics:UIBarMetricsDefault];
}
/** 创建导航栏返回按钮 */
- (void)createLeftBackBarButtonItem {
    
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 15, 25)];
    [leftButton addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];
    [leftButton setImage:[UIImage imageNamed:@"返回按钮"] forState:UIControlStateNormal];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButton;
}

/**创建导航栏有色按钮*/
- (void)_createLeftBackBarButton{
    
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 15, 25)];
    [leftButton addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];
    [leftButton setImage:[UIImage imageNamed:@"New Folder"] forState:UIControlStateNormal];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButton;
}
/** 创建导航栏左侧按钮（图片或文字) */
- (void)createLeftBarButtonItem:(NSString *)name type:(ByNaviItemType)type {
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 45, 25);
    [leftButton addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];

    if (type == ByNaviImageItem) {
        [leftButton setImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
        [leftButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 20)];
    } else if (type == ByNaviTextItem) {
        CGFloat width = [name getSizeOfString:14].width;
        leftButton.frame = CGRectMake(0, 0, width, 25);
        [leftButton setTitle:name forState:UIControlStateNormal];
        [leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [leftButton.titleLabel setFont:FONT(14)];
    }
    
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButton;

}
/** 创建导航栏右侧按钮（图片或文字) */
- (void)createRightBarButtonItem:(NSString *)name type:(ByNaviItemType)type {
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 0, 45, 25);
    [rightButton addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
    
    if (type == ByNaviImageItem) {
        [rightButton setImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
        [rightButton setImageEdgeInsets:UIEdgeInsetsMake(0, 20, 0, 0)];
    } else if (type == ByNaviTextItem) {
        CGFloat width = [name getSizeOfString:14].width;
        rightButton.frame = CGRectMake(0, 0, width, 25);
        [rightButton setTitle:name forState:UIControlStateNormal];
        [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [rightButton.titleLabel setFont:FONT(14)];
    }
    
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightBarButton;
    
}


#pragma mark - 自定义点击事件
/** 左侧按钮点击事件 */
- (void)leftAction:(UIButton *)btn {
    if (self.isModal) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}
/**右侧按钮点击事件*/
- (void)rightAction:(UIButton *)btn{
   
}

// 编辑完成按钮点击事件处理
- (void)keyboardDown
{
    // 使键盘下降
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}



@end
